$(function() {
	
buyfullTempValitate();

if($("#popuperrorMessage").val()==='true'){
	$('#buyfulltemp_add_and_update').modal('show');
}
	
$("body").on("click",'#addbuyfullAmt',function(){
	$("#addHtml").append('<div class="row resetDiv"><div class="col-xs-5 text-left"><input type="text" class="form-control" name="buyfullAmt" id="buyfullAmt"  placeholder="满多少" /></div><div class="col-xs-5 text-left"><input type="text" class="form-control" name="buyfullSubtAmt" id="buyfullSubtAmt"  placeholder="立减多少" /></div><div class="col-xs-2 text-left"><button type="button" class="btn btn-danger deleteRow">删除</button></div></div><br/>');
});

$("body").on("click",'.deleteRow',function(){
	$(this).parent().parent().next().remove();
	$(this).parent().parent().remove();
});

$("body").on("click",'a[name="show"]',function(){
	var buyfullTempId=$(this).attr("id");
	var url="/TblWxBuyfullTemplate/getBuyfullTemplate";
	if(!isEmpty(buyfullTempId)){
		var data = {
					'buyfullTempId' :buyfullTempId,
					};
		var datas=ajaxUtil(url, data, false, "","");
		if(!isEmpty(datas)){
			$("#buyfullTempId").val(datas.data.buyfullTempId);
			$("#buyfullTempName").val(datas.data.buyfullTempName);
			$("#buyfullTempDesc").val(datas.data.buyfullTempDesc);
			$("#buyfullTempStarttimeView").val(datas.data.buyfullTempStarttimeView);
			$("#buyfullTempEndtimeView").val(datas.data.buyfullTempEndtimeView);
			var buyFullList =datas.data.buyFullList;
			if(buyFullList.length>0){
				for(var i=0;i<buyFullList.length;i++){
					if(i==0){
						$("#buyfullAmt").val(buyFullList[i].buyfullAmt);
						$("#buyfullSubtAmt").val(buyFullList[i].buyfullSubtAmt);
						$(".resetDiv").next().remove();
						$(".resetDiv").remove();
					}else{
						$("#addHtml").append('<div class="row resetDiv"><div class="col-xs-5 text-left"><input type="NUMBER" class="form-control" name="buyfullAmt" id="buyfullAmt" value="'+buyFullList[i].buyfullAmt+'"  placeholder="满多少" /></div><div class="col-xs-5 text-left"><input type="NUMBER" class="form-control" name="buyfullSubtAmt" value="'+buyFullList[i].buyfullSubtAmt+'" id="buyfullSubtAmt"  placeholder="立减多少" /></div><div class="col-xs-2 text-left"><button type="button" class="btn btn-danger deleteRow">删除</button></div></div><br/>');
					}
				}
			}
			$('#buyfulltemp_add_and_update').modal('show');
		}else{
			$('#buyfulltemp_add_and_update').modal('hide');
		}
	}
});

$("[name='cleanAll']").click(function(){
	if($(this).attr("type")!="reset"){
		$("#buyfullTempId").val("");
	}
	$(":text").val("");
	$("span[name='message']").text("");
	$("#buyfullAmt").val("");
	$("#buyfullSubtAmt").val("");
	$(".resetDiv").next().remove();
	$(".resetDiv").remove();
	$("#errorMessageAll").remove();
	//Modal验证销毁重构
    $("#formBuyfullTemp").data('bootstrapValidator').destroy();
    $('#formBuyfullTemp').data('bootstrapValidator', null);
    buyfullTempValitate();
});

$("body").on("click",'.example5-3-2', function(){
	var menuUuid=$("input[name='menuUuid']").val();
	var buyfullTempId=$(this).attr("id");
	var url='/TblWxBuyfullTemplate/delete?buyfullTempId='+buyfullTempId+"&menuUuid="+menuUuid;
	deleteAlert(url);
});

});

function buyfullTempValitate(){
	
	$("#formBuyfullTemp").bootstrapValidator({
	    message: 'This value is not valid',
	    feedbackIcons: {
	        valid: 'glyphicon glyphicon-ok',
	        invalid: 'glyphicon glyphicon-remove',
	        validating: 'glyphicon glyphicon-refresh'
	    },
	    fields: {
	 	   buyfullTempName: {
	        	validators: {
	            	notEmpty : {
						message : '模板名称不能为空'
					}
	            }
	        },
	        buyfullTempDesc: {
	            validators: {
	                notEmpty: {
	                    message: '模板介绍不能为空'
	                }
	            }
	        },
	        buyfullTempStarttimeView: {
	     	   message: '开始时间验证失败',
	            trigger:"change", // 监听change动作
	        	validators: {
	            	notEmpty : {
						message : '开始时间不能为空'
					},
					callback: {
	                 message: '开始时间必须大于今天',
	                 callback:function(value, validator,$field){
	                     var begin = $('#buyfullTempStarttimeView').val().replace(/-/g,"/");
	                     var begin_date = new Date(begin);
	                     var today =(new Date()).valueOf();
	                     validator.updateStatus('buyfullTempStarttimeView', 'VALID');
	                     return begin_date>=today;
	                 }
	             }
	        	}
	       },
	       buyfullTempEndtimeView: {
	     	  message: '开结束时间验证失败',
	           trigger:"change", // 监听change动作
	          	validators: {
	              	notEmpty : {
						message : '结束时间不能为空'
					},
					callback: {
	                     message: '结束日期不能小于开始日期',
	                     callback:function(value, validator,$field){
	                         var begin = $('#buyfullTempStarttimeView').val().replace(/-/g,"/");
	                         var begin_date = new Date(begin);
	                         var end =$('#buyfullTempEndtimeView').val();
	                         var end_date = new Date(end);
	                         validator.updateStatus('buyfullTempStarttimeView', 'VALID');
	                         return end_date>=begin_date;
	                     }
	                 }
	          	}
	        },
	       buyfullAmt: {
	        	validators: {
	        		notEmpty : {
							message : '请输入有效金额'
						},
	     			regexp : {
	     				regexp : /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
	     				message : '请输入有效金额'
	     			}
	           }
	        },
	        buyfullSubtAmt: {
	        	validators: {
	        		notEmpty : {
						message : '请输入有效金额'
					},
	     			regexp : {
	     				regexp : /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
	     				message : '请输入有效金额'
	     			}
	           }
	        },
	    },
	    submitHandler: function (validator, form, submitButton) {
	        alert("submit");
	    }
	});

	
}